using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.DoctorsDomain;
using ShivamEMR.Model.DataInterfaces.PersonDataInterf;
using ShivamEMR.Model.DataInterfaces.CompanyDataInterf;
using ShivamEMR.Model.DataInterfaces.DoctorDataInterf;
using ShivamEMR.Model;
using PagedList;
//using ShivamEMR.Web.Areas.Doctor.Models;

namespace ShivamEMR.Web.Areas.Doctor.Controllers
{   
    public class DoctorController : Controller
    {
		private readonly ISexRepository sexRepository;
		private readonly IPersonPictureRepository personpictureRepository;
		private readonly IPersonAccountRepository personaccountRepository;
		private readonly IPersonStatusRepository personstatusRepository;
		private readonly IEmployeeStatusRepository employeestatusRepository;
		private readonly IEmployeeGroupRepository employeegroupRepository;
		private readonly IDoctorRepository doctorRepository;
        private readonly IEmployeeRepository employeeRepository;
        private readonly ISpecialtiesRepository specialtiesRepository;
        private readonly IMaritalStatusRepository maritalstatusRepo;

		// If you are using Dependency Injection, you can delete the following constructor
        public DoctorController()
        {
            sexRepository = Registry.RepositoryFactory.GetSexRepository();
            personpictureRepository = Registry.RepositoryFactory.GetPersonPictureRepository();
            personaccountRepository = Registry.RepositoryFactory.GetPersonAccountRepository();
            personstatusRepository = Registry.RepositoryFactory.GetPersonStatusRepository();
            employeestatusRepository = Registry.RepositoryFactory.GetEmployeeStatusRepository();
            employeegroupRepository = Registry.RepositoryFactory.GetEmployeeGroupRepository();
            doctorRepository = Registry.RepositoryFactory.GetDoctorRepository();
            employeeRepository = Registry.RepositoryFactory.GetEmployeeRepository();
            specialtiesRepository = Registry.RepositoryFactory.GetSpecialtiesRepository();
            maritalstatusRepo = Registry.RepositoryFactory.GetMaritalStatusRepository();
        }

        public DoctorController(ISexRepository sexRepository, IPersonPictureRepository personpictureRepository, IPersonAccountRepository personaccountRepository, IPersonStatusRepository personstatusRepository, IEmployeeStatusRepository employeestatusRepository, IEmployeeGroupRepository employeegroupRepository, IDoctorRepository doctorRepository,IEmployeeRepository employeeRepo, ISpecialtiesRepository specialtiesRepo, IMaritalStatusRepository maritalstatusrepo)
        {
			this.sexRepository = sexRepository;
			this.personpictureRepository = personpictureRepository;
			this.personaccountRepository = personaccountRepository;
			this.personstatusRepository = personstatusRepository;
			this.employeestatusRepository = employeestatusRepository;
			this.employeegroupRepository = employeegroupRepository;
			this.doctorRepository = doctorRepository;
            this.employeeRepository = employeeRepo;
            this.specialtiesRepository = specialtiesRepo;
            this.maritalstatusRepo = maritalstatusrepo;
        }

        //
        // GET: /Doctor/

        public ViewResult Index(string searchString, int? page)
        {
            var doctors = doctorRepository.All;
            if (!String.IsNullOrEmpty(searchString))
            {
                doctors = doctors.Where(p => p.FirstName.ToUpper().Contains(searchString.ToUpper())
                                   || p.MiddleName.ToUpper().Contains(searchString.ToUpper())
                                   || p.LastName.ToUpper().Contains(searchString.ToUpper())
                    || p.Code.ToUpper().Contains(searchString.ToUpper()));
            }
            var plist = doctors.ToList<ShivamEMR.Model.Domain.DoctorsDomain.Doctor>();
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(plist.ToPagedList(pageNumber, pageSize)); 
           // return View(doctorRepository.AllIncluding(doctor => doctor.Sex, doctor => doctor.Picture, doctor => doctor.PersonStatus, doctor => doctor.Addresses, doctor => doctor.Contacts, doctor => doctor.CitizenShips, doctor => doctor.Informations, doctor => doctor.PersonRoles, doctor => doctor.Groups, doctor => doctor.Documents, doctor => doctor.Assignments, doctor => doctor.InteractionsWith, doctor => doctor.EmployeeStatus, doctor => doctor.CurrentBranches, doctor => doctor.Designations, doctor => doctor.Appointments, doctor => doctor.Schedule, doctor => doctor.Interactions, doctor => doctor.Specialties, doctor => doctor.Consultations));
            //return View(doctorRepository.All);
        }

        //
        // GET: /Doctor/Details/5

        public ViewResult Details(System.Guid id)
        {
            return View(doctorRepository.Find(id));
        }

        //
        // GET: /Doctor/Create

        public ActionResult New()
        {
            ViewBag.Employees = employeeRepository.All;
            ViewBag.Specialties = specialtiesRepository.All;
            var doctor=new ShivamEMR.Model.Domain.DoctorsDomain.Doctor(){ DateOfJoin=DateTime.Now,DateOfBirth=DateTime.Now  };
            return View(doctor);
        }

         [HttpPost]
        public ActionResult New(ShivamEMR.Model.Domain.DoctorsDomain.Doctor doctor)
        {
            //doctor =new doctor(){  }  employeeRepository.Find(doctor.ID);
            if (ModelState.IsValid)
            {
                doctorRepository.Create(doctor);
                doctorRepository.Save();
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.Employees = employeeRepository.All;
                ViewBag.Specialties = specialtiesRepository.All;
                return View();
            }
        } 


        //
        // GET: /Doctor/Create

        public ActionResult Create()
        {
			ViewBag.PossibleSexes = sexRepository.All;
			ViewBag.PossiblePictures = personpictureRepository.All;
			ViewBag.PossiblePersonAccounts = personaccountRepository.All;
			ViewBag.PossiblePersonStatus = personstatusRepository.All;
			ViewBag.PossibleEmployeeStatus = employeestatusRepository.All;
			ViewBag.PossibleEmployeeGroups = employeegroupRepository.All;
            ViewBag.Specialties = specialtiesRepository.All;
            ViewBag.PossibleMaritalStatus = maritalstatusRepo.All;
            var doctor = new ShivamEMR.Model.Domain.DoctorsDomain.Doctor() { DateOfJoin = DateTime.Now, DateOfBirth = DateTime.Now };
            return View(doctor);
        } 

        //
        // POST: /Doctor/Create

        [HttpPost]
        public ActionResult Create(ShivamEMR.Model.Domain.DoctorsDomain.Doctor doctor)
        {
            //if (ModelState.IsValid) {
            if(doctor !=null){
                doctorRepository.InsertOrUpdate(doctor);
                doctorRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleSexes = sexRepository.All;
				ViewBag.PossiblePictures = personpictureRepository.All;
				ViewBag.PossiblePersonAccounts = personaccountRepository.All;
				ViewBag.PossiblePersonStatus = personstatusRepository.All;
				ViewBag.PossibleEmployeeStatus = employeestatusRepository.All;
				ViewBag.PossibleEmployeeGroups = employeegroupRepository.All;
                ViewBag.Specialties = specialtiesRepository.All;
                ViewBag.PossibleMaritalStatus = maritalstatusRepo.All;
				return View();
			}
        }
        
        //
        // GET: /Doctor/Edit/5
 
        public ActionResult Edit(System.Guid id)
        {
			ViewBag.PossibleSexes = sexRepository.All;
			ViewBag.PossiblePictures = personpictureRepository.All;
			ViewBag.PossiblePersonAccounts = personaccountRepository.All;
			ViewBag.PossiblePersonStatus = personstatusRepository.All;
			ViewBag.PossibleEmployeeStatus = employeestatusRepository.All;
			ViewBag.PossibleEmployeeGroups = employeegroupRepository.All;
             return View(doctorRepository.Find(id));
        }

        //
        // POST: /Doctor/Edit/5

        [HttpPost]
        public ActionResult Edit(ShivamEMR.Model.Domain.DoctorsDomain.Doctor doctor)
        {
            if (ModelState.IsValid) {
                doctorRepository.InsertOrUpdate(doctor);
                doctorRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleSexes = sexRepository.All;
				ViewBag.PossiblePictures = personpictureRepository.All;
				ViewBag.PossiblePersonAccounts = personaccountRepository.All;
				ViewBag.PossiblePersonStatus = personstatusRepository.All;
				ViewBag.PossibleEmployeeStatus = employeestatusRepository.All;
				ViewBag.PossibleEmployeeGroups = employeegroupRepository.All;
				return View();
			}
        }

        //
        // GET: /Doctor/Delete/5
 
        public ActionResult Delete(System.Guid id)
        {
            return View(doctorRepository.Find(id));
        }

        //
        // POST: /Doctor/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(System.Guid id)
        {
            var doc = doctorRepository.Find(id);
            doctorRepository.Delete(doc);
            doctorRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

